<?php
/**
* @myIPN for sobi 2
* @Copyright (C) 2007 Ethan Chai Voon Chong, http://www.ethansoft.com
* @All Rights Reserved
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class HTML_Myipn {

	function display_entry() 
	{
		global $my, $myipn_config, $database;
	
		echo _MYIPN_EXP_MSG;

		$query = "SELECT configValue FROM #__sobi2_config WHERE configKey='payPalMail' AND sobi2Section='payment'";
                $database->setQuery($query);

                $row =null;

                if ($database->loadObject($row)) {
                              $paypal_email = $row->configValue;
                        }

                echo "<table>";
		$query = "SELECT count(itemid) FROM #__sobi2_item WHERE owner='$my->id'";
		
		$database->setQuery($query);
                $row2 = null;

                if ( $database->loadObject($row2) )
			
		if ($database->loadResult() <= '0') {	
                                echo _MYIPN_NO_ENTRY;
				
                        }
		else
		{
			
			echo "<tr><th align=center>No.</th><th align=center>Title</th>
                        <th align='center'>Hits</th>
                        <th align='center'>Creation Date</th>
                        <th align='center'>Expiration Date</th>
                        <th align='center'>Day(s) left</th>
			<th align='center'>Status</th>
                        <th align='center'>Action</th>
                  	</tr>";

			$total = $database->loadResult();
			
			$query = "SELECT * FROM #__sobi2_item WHERE owner='$my->id'";
			$database->setQuery($query);
			$rows = $database->loadObjectList();
			
			echo "<p align=center>"._MYIPN_TOTAL_MSG."&nbsp;".$total."&nbsp;"._MYIPN_ENTRY."</p>";
			

			foreach($rows as $row)
			{
			 $currency = "";
			 $amount = "";
			 $title = "";
			 $query2 = "SELECT payment_amount, payment_currency FROM #__myipn_paypal_log WHERE entry_number ='$row->itemid'";
                         $database->setQuery($query2);
			
			 $row3 = null;
			 if ($database->loadObject($row3)) 
			 {
                                    $currency = $row3->payment_currency;
				    $amount = $row3->payment_amount;
                         }
			else
			 { 
				$query2 = "SELECT amount, currency FROM #__myipn_entry_log WHERE itemid='$row->itemid'";
				$database->setQuery($query2);
	                        $rows3 = null;
				if ($database->loadObject($rows3))
                         	{ 
					   
					$currency = $rows3->currency;
                                	$amount = $rows3->amount;
			 	}	
			}
			
			echo "<tr><td align=center width=3%>".$row->itemid."</td>";
			$current_time = getTimeAndDate();
			if($row->published == 0)
			$date_diff = ' - ';
			else
			$date_diff = (int)((strtotime("$row->publish_down")-strtotime("$current_time"))/86400);
        		echo "<td align=center width='30%'>".$row->title."</td>";
			echo "<td align=center width='3%'>".$row->hits."</td>";
			echo "<td align=center width='16%'>".$row->publish_up."&nbsp;</td>";
			if ($row->approved == 1)		
				echo "<td align=center width='16%'>".$row->publish_down."&nbsp;</td>";
			else
				echo "<td align=center width='16%'>-&nbsp;</td>";
		
			echo "<td align=center width='7%'>".$date_diff."</td>";
			if ($row->publish_down > $current_time && $row->approved == 1)
			{
				echo "<td align=center width=5%><b>Active</b></td>";
				$title = "Renewal of Entry ".$row->title;
			}
			else if ($current_time > $row->publish_down && $row->approved == 1 )
			{
				echo "<td align=center width=5%><b><font color='red'>Expired</font></b></td>";
				$title = "Renewal of Entry ".$row->title;
			}
			else
			{
				echo "<td align=center width=5%><b><font color='red'>Pending</font></b></td>";
				$title = "Payment of Entry ".$row->title;
			}
			if ($myipn_config->sandbox == 1)
				$action_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
			else
				$action_url = "https://www.paypal.com/cgi-bin/webscr";
			if($amount != "" && $currency != ""){
			$html = "<td align=center width=10%><form name='checkout' method='post' action='".$action_url."'>";
                	$html .= "<input type='hidden' name='business' value='".$paypal_email."'>";
                	$html .= "<input type='hidden' name='tax' value='0'>";
                	$html .= "<input type='hidden' name='no_note' value='1'>";
                	$html .= "<input type='hidden' name='no_shipping' value='1'>";
                	$html .= "<input type='hidden' name='cmd' value='_xclick'>";
                	$html .= "<input type='hidden' name='rm' value='2'>";
                	$html .= "<input type='hidden' name='custom' value=".$my->username.">";
                	$html .= "<input type='hidden' name='currency_code' value='".$currency."'>";
               	 	$html .= "<input type='hidden' name='notify_url' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=paypal' />";
                	$html .= "<input type='hidden' name='return' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=complete'>";
                	$html .= "<input type='hidden' name='cancel_return' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=cancel'>";
                	$html .= "<input type='hidden' name='item_name' value='".$title."'>";
                	$html .= "<input type='hidden' name='item_number' value='".$row->itemid."'>";
                	$html .= "<input type='hidden' name='amount' value='".$amount."'>";
                	$html .= "<input type='submit' name='submit' value='Pay/Renew' class='button'>";
                	$html .= "</form></td>";
                	echo $html;
			}
			else
			echo "<td>Free Entry</td>";

			echo "</tr>";
    			}
			} 
		echo "</table>";

	}
	
	// Display Thank you message on finishing check out from Paypal 
	function display_complete() { 
	global $my;
?>
		<p><b><font color="#FF0000"><?php echo _MYIPN_THANKS; ?></font></b><br /><br />
		<?php echo _MYIPN_COMPLETE; ?></p>
	
	<?php
		}
	
	// Display Cancel  message on if user click cancel from Paypal
	function display_cancel() {
	global $my;
?>
		<p><b><font color="#FF0000"><?php echo _MYIPN_CANCELLED; ?></font></b><br />
		<br />
		<?php echo _MYIPN_TRY_LATER; ?></p>
<?php
	}

}

?>
